package com.jcx.ldzj.utile;

import com.itextpdf.text.*;
import com.itextpdf.text.html.simpleparser.HTMLWorker;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Iterator;

public class CreatePDFUtile {



    /**
     * @throws FileNotFoundException
     * @throws DocumentException
     */
    /**
     *
     * 生成pdf到对应目录文件
     * @param path 可以是相对路径，以/结尾
     * @param name 名称
     * @param Para pdf所加数据
     * @throws FileNotFoundException
     * @throws DocumentException
     */
    public static void createPDF(String path,String name,Paragraph Para) throws FileNotFoundException, DocumentException {
        //创建一个画布
        Document document = new Document();
        //创建pdf
        PdfWriter.getInstance(document, new FileOutputStream(path+name));
        //打开pdf
        document.open();
        //为pdf新增数据
        document.add(Para);
        //关闭流
        document.close();
    }

    /**
     * 设置密码
     * @param outpath 文件地址
     * @throws DocumentException
     * @throws FileNotFoundException
     */
    public static void setPDF(String outpath) throws DocumentException, FileNotFoundException {
        //创建一个画布
        Document doc = new Document();
        PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(outpath));

        // 设置密码为："World"
        writer.setEncryption("Hello".getBytes(), "World".getBytes(),
                PdfWriter.ALLOW_SCREENREADERS,
                PdfWriter.STANDARD_ENCRYPTION_128);

        doc.open();
        doc.add(new Paragraph("Hello World"));
    }

    /**
     * 合并pdf
     * @param pathname1 第一个pdf路径
     * @param patname2 第二个pdf路径
     * @param pathname3 合并的pdf路径
     * @throws IOException
     * @throws DocumentException
     */
    public static void merge(String pathname1,String patname2,String pathname3) throws IOException, DocumentException {
        PdfReader reader1 = new PdfReader(pathname1);
        PdfReader reader2 = new PdfReader(patname2);

        FileOutputStream out = new FileOutputStream(pathname3);

        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, out);

        document.open();
        PdfContentByte cb = writer.getDirectContent();

        java.util.List<PdfReader> readers = new ArrayList<PdfReader>();
        readers.add(reader1);
        readers.add(reader2);
        int pageOfCurrentReaderPDF = 0;
        Iterator<PdfReader> iteratorPDFReader = readers.iterator();
        while (iteratorPDFReader.hasNext()) {
            PdfReader pdfReader = iteratorPDFReader.next();

            // Create a new page in the target for each source page.
            while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
                document.newPage();
                pageOfCurrentReaderPDF++;
                PdfImportedPage page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
                cb.addTemplate(page, 0, 0);
            }
            pageOfCurrentReaderPDF = 0;
        }
        out.flush();
        document.close();
        out.close();
    }

    /**
     * html转成pdf
     * @param pdfpath pdf路径
     * @param htmlpath html路径
     * @throws IOException
     * @throws DocumentException
     */
    public static void htmlTopdf(String pdfpath,String htmlpath) throws IOException, DocumentException {
        Document document = new Document(PageSize.LETTER);
        PdfWriter.getInstance(document, new FileOutputStream(pdfpath));
        document.open();
        HTMLWorker htmlWorker = new HTMLWorker(document);
        htmlWorker.parse(new StringReader(htmlpath));
        document.close();
    }
}
